package com.example.arm.common.lifecycle

import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner

/**
 *  author : yanghaozhang
 *  date : 2022/12/27 14:04
 *  description :
 */

sealed class BaseLifecycleWithOwnerBinder(
    private val onCreateFunction: LifecycleWithOwnerCallBack? = null,
    private val onStartFunction: LifecycleWithOwnerCallBack? = null,
    private val onResumeFunction: LifecycleWithOwnerCallBack? = null,
    private val onPauseFunction: LifecycleWithOwnerCallBack? = null,
    private val onStopFunction: LifecycleWithOwnerCallBack? = null,
    private val onDestroyFunction: LifecycleWithOwnerCallBack? = null
) : DefaultLifecycleObserver {

    override fun onCreate(owner: LifecycleOwner) {
        super.onCreate(owner)
        onCreateFunction?.invoke(owner)
    }

    override fun onStart(owner: LifecycleOwner) {
        super.onStart(owner)
        onStartFunction?.invoke(owner)
    }

    override fun onResume(owner: LifecycleOwner) {
        super.onResume(owner)
        onResumeFunction?.invoke(owner)
    }

    override fun onPause(owner: LifecycleOwner) {
        super.onPause(owner)
        onPauseFunction?.invoke(owner)
    }

    override fun onStop(owner: LifecycleOwner) {
        super.onStop(owner)
        onStopFunction?.invoke(owner)
    }

    override fun onDestroy(owner: LifecycleOwner) {
        super.onDestroy(owner)
        onDestroyFunction?.invoke(owner)
        owner.lifecycle.removeObserver(this)
    }
}

class CreateWithOwnerBinder(onCreate: LifecycleWithOwnerCallBack?) : BaseLifecycleWithOwnerBinder(onCreate)
class StartWithOwnerBinder(onStart: LifecycleWithOwnerCallBack?) : BaseLifecycleWithOwnerBinder(onStartFunction = onStart)
class ResumeWithOwnerBinder(onResume: LifecycleWithOwnerCallBack?) : BaseLifecycleWithOwnerBinder(onResumeFunction = onResume)
class PauseWithOwnerBinder(onPause: LifecycleWithOwnerCallBack?) : BaseLifecycleWithOwnerBinder(onPauseFunction = onPause)
class StopWithOwnerBinder(onStop: LifecycleWithOwnerCallBack?) : BaseLifecycleWithOwnerBinder(onStopFunction = onStop)
class DestroyWithOwnerBinder(onDestroy: LifecycleWithOwnerCallBack?) : BaseLifecycleWithOwnerBinder(onDestroyFunction = onDestroy)
